package org.vector.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.table.DefaultTableModel;

import org.vector.daoImpl.BookDaoImpl;
import org.vector.serviceImpl.BookServiceImpl;

/**
 *
 * @author 73786
 */
@SuppressWarnings("serial")
public class WinTypeset extends javax.swing.JFrame {
	BookDaoImpl bdi = new BookDaoImpl();
	String[][] date = null;
	String[][] date2 = null;
	BookServiceImpl bsi = new BookServiceImpl();
	
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JButton jButton4;
	private javax.swing.JButton jButton5;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JTextField jTextField3;

	public WinTypeset() {
		initComponents();
		setVisible(true);
	}

	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jTextField2 = new javax.swing.JTextField();
		jTextField3 = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();
		jButton4 = new javax.swing.JButton();
		jButton5 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("图书类型管理");
		setBounds(500, 275, 673, 583);
		
		jLabel1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jLabel1.setText("图书类型");

		jTextField1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

		jTable1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jTable1.setModel(new javax.swing.table.DefaultTableModel(
				bdi.getBookType(),
				new String[] { "图书类别", "图书名称" }) {
			boolean[] canEdit = new boolean[] { false, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		jTable1.setRowHeight(30);
		jScrollPane1.setViewportView(jTable1);

		jLabel3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jLabel3.setText("图书类型名称");

		jLabel4.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jLabel4.setText("图书类型编号");

		jTextField2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

		jTextField3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N

		jButton1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jButton1.setText("添加");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jButton2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jButton2.setText("修改");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jButton3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jButton3.setText("退出");
		jButton3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton3ActionPerformed(evt);
			}
		});

		jButton4.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jButton4.setText("删除");
		jButton4.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton4ActionPerformed(evt);
			}
		});

		jButton5.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
		jButton5.setText("查询");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 442,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jButton5))
								.addGroup(layout.createSequentialGroup().addGap(77, 77, 77).addGroup(layout
										.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(
												jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 501,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGroup(layout.createSequentialGroup().addGroup(layout
												.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
												.addComponent(jButton1)
												.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 125,
														Short.MAX_VALUE)
												.addComponent(
														jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
												.addGap(20, 20, 20)
												.addGroup(layout
														.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(layout.createSequentialGroup().addGap(28, 28, 28)
																.addGroup(layout.createParallelGroup(
																		javax.swing.GroupLayout.Alignment.LEADING)
																		.addComponent(jTextField3,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				304,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(
																				jTextField2,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				304,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
														.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
																layout.createSequentialGroup().addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(jButton2).addGap(74, 74, 74)
																		.addComponent(jButton4).addGap(47, 47, 47)
																		.addComponent(jButton3)))))))
						.addContainerGap(54, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
				.createSequentialGroup().addContainerGap()
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
								.addGroup(layout.createSequentialGroup().addComponent(jTextField1).addGap(2, 2, 2))
								.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING,
										javax.swing.GroupLayout.PREFERRED_SIZE, 32,
										javax.swing.GroupLayout.PREFERRED_SIZE))
						.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
								javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(18, 18, 18)
				.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 229,
						javax.swing.GroupLayout.PREFERRED_SIZE)
				.addGap(44, 44, 44)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
						.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
								javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(28, 28, 28)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
						.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
								javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(45, 45, 45)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jButton1)
						.addComponent(jButton3).addComponent(jButton4).addComponent(jButton2))
				.addContainerGap(51, Short.MAX_VALUE)));

		jTable1.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) {}
			@Override
			public void mousePressed(MouseEvent arg0) {}
			@Override
			public void mouseExited(MouseEvent arg0) {}
			@Override
			public void mouseEntered(MouseEvent arg0) {}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				//获取表格中被选中的行
				int row = jTable1.getSelectedRow();
				date = bdi.getBookType();
				//设置文本框的内容 
				jTextField3.setText(date[row][0]);
				jTextField2.setText(date[row][1]);
			}
		});
		jButton5.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				date2= bdi.getBookTypeOne(jTextField1.getText());
				if(date2 == null) {
		    		new WinTip("未找到该种类，换一下试试");
		    	}else {
		    		DefaultTableModel dataModel = new DefaultTableModel(
		            		date2,
		            		new String[] { "图书类别", "图书名称" });
		        	jTable1.setModel(dataModel);
		        	
		        	jTable1.addMouseListener(new MouseListener() {
		    			@Override
		    			public void mouseReleased(MouseEvent arg0) {}
		    			@Override
		    			public void mousePressed(MouseEvent arg0) {}
		    			@Override
		    			public void mouseExited(MouseEvent arg0) {}
		    			@Override
		    			public void mouseEntered(MouseEvent arg0) {}
		    			@Override
		    			public void mouseClicked(MouseEvent arg0) {
		    				//获取表格中被选中的行
		    				int row = jTable1.getSelectedRow();
		    				//date = bdi.getBookType();
		    				//设置文本框的内容 
		    				jTextField3.setText(date2[row][0]);
		    				jTextField2.setText(date2[row][1]);
		    			}
		    		});
		    	}
			}
		});
		pack();
	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		//修改
		int row = jTable1.getSelectedRow();
		date = bdi.getBookType();
		String aa = date[row][1];
		String q = jTextField3.getText();
		String w = jTextField2.getText();
		int rows = bsi.ChangeBookType(aa, q, w);
		if(rows == 1) {
			WinTypeset.this.dispose();
			new WinTypeset();
			new WinTip("修改成功");
		}else {
			new WinTip("修改失败");
		}
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		//添加
		String q = jTextField3.getText();
		String w = jTextField2.getText();
		int row = bsi.BookTypeAdd(q, w);
		if(row == 1) {
			WinTypeset.this.dispose();
			new WinTypeset();
			new WinTip("添加成功");
		}else {
			new WinTip("添加失败");
		}
	}

	private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
		//删除
		int row = jTable1.getSelectedRow();
		date = bdi.getBookType();
		String aa = date[row][1];
		int rows = bsi.DeleteBookType(aa);
		if(rows == 1) {
			WinTypeset.this.dispose();
			new WinTypeset();
			new WinTip("删除成功");
		}else {
			new WinTip("删除失败");
		}
	}

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
		WinTypeset.this.dispose();
	}

	public static void main(String args[]) {
		new WinTypeset();
	}

}
